
<template>
    <div id="app">
        <div class="main-bar-view">

            <img src="../assets/userlogo.png" class="logo">

            <div class="search-entry">
                <img src="../assets/search-icon.svg" class="search-icon">
                <input class="search-input" placeholder="请输入搜索的内容" v-model="searchValue" @keydown.enter="handleEnter()">
            </div>
            <div class="right-view">

                <a href="/home" class="manage">
                    退出登录
                </a>
                <a class="ant-dropdown-link ant-dropdown-trigger" v-if="isLogged" @click="toUserCenter">
                  <img src="../assets/images/avatar.jpg" class="self-img">
                </a>
                <button class="login btn hidden-sm" @click="toLoginPage" v-else>登录</button>
                <div class="right-icon">
                    <img src="../assets/message-icon.svg" @click="toMessage">
                </div>
            </div>
        </div>
        <div class="content">
            <div class="content-left">
                <div class="left-search-item">
                    <h4>图书分类</h4>
                    <ul role="tree" unselectable="on" class="ant-tree" style="min-height: 220px;">
                        <li role="treeitem" class="ant-tree-treenode-switcher-close">
                            <span class="ant-tree-switcher ant-tree-switcher-noop"></span>
                            <span title="全部" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-normal">
                                <span title="ant-tree-title" @click="handleAll">全部</span>
                            </span>
                        </li>
                        <li role="treeitem" class="ant-tree-treenode-switcher-close">
                            <span class="ant-tree-switcher ant-tree-switcher-noop"></span>
                            <span title="都市" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-normal">
                                <span title="ant-tree-title" @click="handleCategory('自传')">自传</span>
                            </span>
                        </li>
                        <li role="treeitem" class="ant-tree-treenode-switcher-close">
                            <span class="ant-tree-switcher ant-tree-switcher-noop"></span>
                            <span title="玄幻" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-normal">
                                <span title="ant-tree-title" @click="handleCategory('教学资料')">教学资料</span>
                            </span>
                        </li>
                        <li role="treeitem" class="ant-tree-treenode-switcher-close">
                            <span class="ant-tree-switcher ant-tree-switcher-noop"></span>
                            <span title="修真" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-normal">
                                <span title="ant-tree-title" @click="handleCategory('科幻')">科幻</span>
                            </span>
                        </li>
                        <li role="treeitem" class="ant-tree-treenode-switcher-close">
                            <span class="ant-tree-switcher ant-tree-switcher-noop"></span>
                            <span title="网游" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-normal">
                                <span title="ant-tree-title" @click="handleCategory('古典文学')">古典文学</span>
                            </span>
                        </li>
                        <li role="treeitem" class="ant-tree-treenode-switcher-close">
                            <span class="ant-tree-switcher ant-tree-switcher-noop"></span>
                            <span title="科幻" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-normal">
                                <span title="ant-tree-title" @click="handleCategory('其他')">其他</span>
                            </span>
                        </li>
                        <li role="treeitem" class="ant-tree-treenode-switcher-close">
                            <span class="ant-tree-switcher ant-tree-switcher-noop"></span>
                            <span title="穿越" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-normal">
                                <span title="ant-tree-title" @click="handleCategory('励志')">励志</span>
                            </span>
                        </li>
                        <li role="treeitem" class="ant-tree-treenode-switcher-close">
                            <span class="ant-tree-switcher ant-tree-switcher-noop"></span>
                            <span title="穿越" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-normal">
                                <span title="ant-tree-title" @click="handleCategory('哲学')">哲学</span>
                            </span>
                        </li>
                        <li role="treeitem" class="ant-tree-treenode-switcher-close">
                            <span class="ant-tree-switcher ant-tree-switcher-noop"></span>
                            <span title="穿越" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-normal">
                                <span title="ant-tree-title" @click="handleCategory('经济')">经济</span>
                            </span>
                        </li>
                        <li role="treeitem" class="ant-tree-treenode-switcher-close">
                            <span class="ant-tree-switcher ant-tree-switcher-noop"></span>
                            <span title="穿越" class="ant-tree-node-content-wrapper ant-tree-node-content-wrapper-normal">
                                <span title="ant-tree-title" @click="handleCategory('读者互借')">读者互借</span>
                            </span>
                        </li>

                    </ul>

                </div>
                <div class="left-search-item">
                     <h4>热门标签</h4>
                     <div class="tag-view tag-flex-view">
                        <span class="tag">电影</span>
                        <span class="tag">音乐</span>
                        <span class="tag">散文</span>
                        <span class="tag">教育</span>
                        <span class="tag">计算机算法</span>
                        <span class="tag">法律</span>
                        <span class="tag">旅行</span>
                        <span class="tag">科研</span>
                        <span class="tag">java</span>
                        <span class="tag">美食</span>
                        <span class="tag">旅行</span>
                        <span class="tag">玄幻</span>
                        <span class="tag">科幻</span>
                        <span class="tag">育儿</span>
                     </div>
                </div>
            </div>

            <div class="content-right">
               <div class="top-select-view flex-view">
               </div>
               <div class="ant-spin-nested-loading" style="min-height: 200px;max-width: 1100px;">
                  <div class="ant-spin-container">
                    <div class="pc-book-list flex-view">
                       <div v-for="(bookInfo,bookTitle) in paginatedBooks" :key="bookTitle" class="book-item item-column-3" @click="handleClick(bookInfo.bookid)">
                          <div class="img-view">
                              <img :src="bookInfo.coverURL">
                           </div>
                         <div class="info-view">
                             <h3 class="book-name">{{ bookTitle }}</h3>
                           </div>
                       </div>
                    </div>
                  </div>
               </div>

              <div class="content-pagination">
                           <el-pagination
                          background="blue"
                            layout="prev, pager, next"
                            :total="totalItems"
                            :page-size="pageSize"
                            @current-change="handleCurrentChange"
                        ></el-pagination>
                      </div>
            </div>

            <div class="content-rright" style="max-width: 150px ;justify-content: flex-end;margin-top: 100px" >
              <div style="display: flex;align-items: center;">
                  <div class="el-icon-s-platform"></div>
                         <h4 style="margin: 0">在线阅读</h4>
              </div>

                        <div style="margin-top: 10px">
                          <p>已经迫不及待想读书了？点击进入我们在线阅读模式 click=></p>
                          <button  style="width: 120px;height: 40px;margin-top: 10px;background-color: rgb(64,158,255);border: none;color: white" @click="bookRecClick">点击进入</button>
                        </div>

                        <div style="display: flex;align-items: center;margin-top: 50px">
                             <div class="el-icon-s-promotion" ></div>
                         <h4 style="margin-bottom:0;margin-top: 0">图书推荐</h4>
                        </div>

                        <div>
                          <p style="margin-top: 10px">书架空空，找不到想读的书了？快来和我们的AI聊天机器人聊聊获取推荐图书 click=></p>
                          <button style="width: 120px;height: 40px;margin-top: 10px;background-color: rgb(64,158,255);border: none;color: white" @click="toRecommend">点击进入</button>
                        </div>
            </div>

        </div>
        <div class="footer-view">
            <div class="foot-link-box ">

                    <a href="" class="foot-link" target="_blank">后台管理</a>


                    <a href="" class="foot-link" target="_blank">关于我们</a>

            </div>
            <div class="footer-infos">
                <sapn style="color: rgb(174, 174, 174);">京ICP备12345678号</sapn>
                <span style="color: rgb(174, 174, 174);">  &nbsp; 京公网安备123456  &nbsp; &nbsp;新出发京零字第东123456号   &nbsp; &nbsp;统一社会信用代码 123456789</span>
            </div>
            <div class="address">
                <span style="color: rgb(174, 174, 174);">2024-2025 © 湖南代码演示 · All Rights Reserved</span>
            </div>
        </div>


    </div>

</template>

<style>

.main-bar-view {
    position: fixed;
    top: 0;
    left: 0;
    height: 56px;
    width: 100%;
    background: #fff;
    border-bottom: 1px solid #cedce4;
    padding-left: 48px;
    z-index: 16;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    -ms-flex-direction: row;
    flex-direction: row;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
}
.main-bar-view .logo {
    width: 30px;
    height: auto;
    margin-right: 24px;
}
.search-entry {
    display: flex;
    position: relative;
    width: 400px;
    min-width: 200px;
    height: 32px;
    background: #ecf3fc;
    padding: 0 12px;
    border-radius: 16px;
    font-size: 0;
    cursor: pointer;
}
.search-entry .search-icon{
  justify-content: flex-start;
  width: 18px;
}
.search-entry img{
  justify-content: flex-start;
  max-width: 100%;
  height: auto;
}
.search-input{
    position: absolute;
    top: -5px;
    left: 40px;
    width: auto;
    height: 24px;
    border: 0;
    outline: none;
    color: #000;
    background: #ecf3fc;
    font-size: 14px;
    padding: 0;
}
.right-view{
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: rgba(0,0,0,0);
    --antd-wave-shadow-color: #1890ff;
    color: rgba(0,0,0,.65);
    font-size: 14px;
    font-variant: tabular-nums;
    line-height: 1.5;
    font-feature-settings: "tnum";
    font-family: Avenir,Helvetica,Arial,sans-serif;
    box-sizing: border-box;
    padding-right: 80px;
    -webkit-box-flex: 1;
    flex: 1;
    display: flex;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    flex-direction: row;
    gap: 20px;
    -webkit-box-pack: end;
    justify-content: flex-end;

}
.self-img {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    vertical-align: middle;
    cursor: pointer;
  }
a{
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: rgba(0,0,0,0);
    --antd-wave-shadow-color: #1890ff;
    font-size: 14px;
    font-variant: tabular-nums;
    font-feature-settings: "tnum";
    font-family: Avenir,Helvetica,Arial,sans-serif;
    box-sizing: border-box;
    color: #1890ff;
    text-decoration: none;
    background-color: transparent;
    outline: none;
    cursor: pointer;
    transition: color .3s;
    touch-action: manipulation;
    line-height: 32px;
    width: 60px;

}
.right-view .btn {
    background: #4684e2;
    font-size: 14px;
    color: #fff;
    border-radius: 32px;
    text-align: center;
    width: 66px;
    height: 32px;
    line-height: 32px;
    vertical-align: middle;
    margin-left: 32px;
}
.right-view button {
    outline: none;
    border: none;
    cursor: pointer;
}
.right-view img[data-v-97d8b1e2] {
    cursor: pointer;
}
.right-view .right-icon img{
    margin-right: 20px;
    position: relative;
    width: 24px;
    margin: 4px 0 0 4px;
    cursor: pointer;
    display: inline-block;
    font-size: 0;
}
.content-left {
    width: 240px;
    margin-right: 32px;
    margin-top: 50px;
    margin-left: 20px;
}
.left-search-item {
    width:240px;
    overflow: hidden;
    border-bottom: 1px solid #cedce4;

    padding-bottom: 24px;
}
.content {
    display: flex;
   margin-top: 50px;
}
h4 {
    color: #4d4d4d;
    font-weight: 600;
    font-size: 16px;
    line-height: 24px;
    height: 24px;
}
.ant-tree {

    box-sizing: border-box;
    color: rgba(0, 0, 0, .65);
    font-size: 14px;
    font-variant: tabular-nums;
    line-height: 1.5;
    list-style: none;
    -webkit-font-feature-settings: "tnum";
    font-feature-settings: "tnum";
    margin: 0;
    padding: 0;
}
.ant-tree li {

    margin: 0;
    padding: 2px 0;
    white-space: nowrap;
    list-style: none;
    outline: 0;
    background-color: rgb(234,241,245);
    margin-top: 5px;
    border-radius: 5px;
}
.ant-tree li span.ant-tree-iconEle, .ant-tree li span.ant-tree-switcher {
    display: inline-block;
    width: 24px;
    height: 24px;
    margin: 0;
    line-height: 24px;
    text-align: center;
    vertical-align: top;
    border: 0 none;
    outline: none;
    cursor: pointer;
}
.ant-tree li .ant-tree-node-content-wrapper {
    display: inline-block;
    height: 24px;
    margin: 0;
    padding: 0 5px;
    color: rgba(0, 0, 0, .65);
    line-height: 24px;
    text-decoration: none;
    vertical-align: top;
    border-radius: 2px;
    cursor: pointer;
    -webkit-transition: all .3s;
    transition: all .3s;
}
h4 {
    color: #4d4d4d;
    font-weight: 600;
    font-size: 16px;
    line-height: 24px;
    height: 24px;
}
.tag-view {

    flex-wrap: wrap;
    margin-top: 4px;

}
.tag-flex-view {

    display: flex;

}
.tag {

    border: 1px solid #a1adc6;
    box-sizing: border-box;
    border-radius: 16px;
    height: 20px;
    line-height: 18px;
    padding: 0 8px;
    margin: 8px 8px 0 0;
    cursor: pointer;
    font-size: 14px;
    color: #152833;
}
.content-right {



  max-width: 1130px;
    flex: 1;
    padding-top: 12px;
    margin-top: 24px;
}
.content-right .top-select-view {
  display: flex;

   justify-content: flex-start;
    align-items: flex-start;
    height: 40px;
    line-height: 40px;
}
body {
    margin: 0;
    color: rgba(0, 0, 0, .65);
    font-size: 16px;
    font-family: -apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol;
    font-variant: tabular-nums;
    line-height: 1.5;
    background-color: #fff;
    -webkit-font-feature-settings: "tnum";
    font-feature-settings: "tnum";
}

.content-right .pc-book-list .book-item {
    min-width: 255px;
    max-width: 255px;
    min-height: 300px;
    position: relative;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    margin-right: 20px;

    height: fit-content;
    border-radius: 4px;
    overflow: hidden;
    margin-top: 16px;
    cursor: pointer;
}

.content-right .pc-book-list .book-item .img-view img {

    height: 210px;
    width: auto;
    display: block;
    margin: 0 auto;
    border-radius: 4px;

    box-sizing: border-box;
}
.ant-spin-container {
    position: relative;
    -webkit-transition: opacity .3s;
    transition: opacity .3s;
}
.pc-book-list {
    display: flex;
    flex-wrap: wrap; /* 允许元素换行 */
}
.img-view{
    background-color:rgb(234,241,245)
}
.info-view{
    background-color: rgb(246,249,251);
    position: relative;
    width: 100%;
    height:100px;
  text-align:center;
}

p {
    margin: 0; /* 将段落的外边距设置为零 */
    text-align: center;
}
h3{
    margin-top: 0;
    margin-left: 20px;
    text-align: center;
    width: 200px;
    font-size: large;
}

.footer-view {
    text-align: center; /* 设置文本内容水平居中对齐 */
    margin-top: 100px;
}

.footer-view .foot-link-box {
    margin-bottom: 20px; /* 可选：添加一些底部间距 */
}

.footer-view .footer-infos{

}
.footer-view .address {
    margin-bottom: 40px; /* 可选：添加一些底部间距 */
}

.footer-view .footer-infos span,
.footer-view .address span {
    color: rgb(174, 174, 174); /* 设置文字颜色 */
}

/* 以下是可选的样式，用于调整文本内容之间的间距 */
.footer-view .foot-link-box a {
    margin: 0 10px; /* 可选：添加左右间距 */
}
.content-pagination{
  margin-top: 50px;
   display: flex;

  margin-left: 300px;
}

</style>
<script>
import axios from "axios";
import Cookies from "js-cookie";

export default {
  data (){
    return{
      booksData:[],
      currentPage: 1,
      pageSize: 20,
      categoryName:'',
      searchValue: ''
    }
  },
  methods:{
    handleCurrentChange(page) {
      this.currentPage = page;
    },
    fetchBooksData(){
      axios.get('http://127.0.0.1:5000/v1/books/allBookCover', {
      headers: {
        'token': `Bearer ${Cookies.get('token')}`
               }
      })
         .then(response => {
          this.booksData=response.data.data
          console.log(this.booksData)
        })
        .catch(() => {
           alert("数据异常")
        });
    },
    handleAll(){
      this.fetchBooksData()
      console.log(this.paginatedBooks)
    },
    handleHottest(){
       axios.get('http://127.0.0.1:5000/v1/booksOnline/getHottestCoverURL', {
              headers: {
                    'token': `Bearer ${Cookies.get('token')}`
              }
       })
        .then(response => {
          this.booksData=response.data.data
          console.log(this.booksData)

        })
        .catch(() => {
           alert("数据异常")
        });
    },
    handleRecommend(){
         axios.get('http://127.0.0.1:5000/v1/booksOnline/getRecommendCoverURL', {
              headers: {
                    'token': `Bearer ${Cookies.get('token')}`
              }
       })
        .then(response => {
          this.booksData=response.data.data
          console.log(this.booksData)

        })
        .catch(() => {
           alert("数据异常")
        });
    },
    handleCategory(categoryName){
      console.log(categoryName)
         axios.post('http://127.0.0.1:5000/v1/books/getBookCoverByCategory', { category:categoryName},{
           headers: {
                    'token': `Bearer ${Cookies.get('token')}`
              }
         })
        .then(response => {
          if (response.data.status === 200){
            this.booksData = response.data.data
            console.log(this.booksData)
          }else{
            alert('未获取到该分类书籍')
          }

        })
        .catch(() => {
           alert("数据异常")
        });
    },
    handleClick(bookTitle){
       this.$router.push('/detail/'+bookTitle);
    },
    handleEnter(){
      axios.post('http://127.0.0.1:5000/v1/books/searchBook',
          {'keyword':this.searchValue},
          {headers: {
                    'token': `Bearer ${Cookies.get('token')}`,
                    'Content-Type': 'application/json'
              }})
          .then(response => {
            if (response.data.status === 200)
            {
              this.booksData = response.data.data;
            }
            else {
              this.$message.warning('未找到相关书籍');
            }
          })
    },
    bookRecClick()
    {
       this.$router.push('/userview');
    },
    toLoginPage(){
        this.$router.push('/login')
    },
    toRecommend(){
      this.$router.push('/recommend')
    },
    toUserCenter(){
      const message = "this is not admin";
      this.$router.push({ name: 'person', params: { message }});
    },
    toMessage(){
      this.$router.push('/notice')
    }

  },
  mounted() {
    this.fetchBooksData() ;
    //   发送异步请求获取数据


  },
  computed: {
    totalItems() {
      return Object.keys(this.booksData).length;
      // eslint-disable-next-line no-unreachable

    },
    paginatedBooks() {
      const startIndex = (this.currentPage - 1) * this.pageSize;
      const endIndex = startIndex + this.pageSize;
      return  Object.fromEntries(Object.entries(this.booksData).slice(startIndex, endIndex));
    },
    isLogged(){
      const token = Cookies.get('token');
      console.log(token)
      const isLogged = token !== undefined && token !== '';
      console.log(isLogged)
      return isLogged;
    }

  },
}
</script>